package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import ModelLayer.ZipCode;

public class DbZipCode implements IfDbZipCode
{
	private Connection con;
	
	public DbZipCode()
	{
		con = DbConnection.getInstance().getDBcon();
	}

	public ArrayList<ZipCode> getAllZipCodes(boolean retriveAssociation) 
	{
        return miscWhere("", retriveAssociation);
	}

	private ArrayList<ZipCode> miscWhere(String wClause, boolean retrieveAssociation)
	{
        ResultSet results;
	    ArrayList<ZipCode> list = new ArrayList<ZipCode>();	
		
	    String query =  buildQuery(wClause);
  
        try
        {
        	Statement stmt = con.createStatement();
        	stmt.setQueryTimeout(5);
        	results = stmt.executeQuery(query);
	 	
        	while( results.next() )
        	{
        		ZipCode zipcodeObject = new ZipCode();
        		zipcodeObject = buildZipCode(results);	
                list.add(zipcodeObject);	
        	}
            stmt.close();     
		}
	 	catch(Exception e){
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}

	private ZipCode singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		ZipCode zipcodeObject = new ZipCode();
                
	        String query =  buildQuery(wClause);
                System.out.println(query);
		try
		{ 
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() )
	 		{
                            zipcodeObject = buildZipCode(results);
                            stmt.close();
			}
                        else
                        {
                            zipcodeObject = null;
                        }
		}	
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return zipcodeObject;
	}
	
	private String buildQuery(String wClause)
	{
	    String query="SELECT zipcode FROM ZipCode";
		
		if (wClause.length()>0)
		{
			query=query+" WHERE "+ wClause;
		}			
		return query;
	}

	private ZipCode buildZipCode(ResultSet results)
    {   
		ZipCode zipcodeObject = new ZipCode();
        try
        {
            zipcodeObject.setZipcode(results.getString("ZipCode"));
        }
        catch(Exception e)
        {
            System.out.println("There is a failure in building the ZipCode");
        }
        return zipcodeObject;
    }
	
	public ZipCode findZipCodeByZipCode(String zipcode, boolean retriveAssociation) 
	{
		String wClause = "ZipCode = '" + zipcode + "'";
        System.out.println("SearchInvoice " + wClause);
        return singleWhere(wClause, retriveAssociation);
	}

	public int insertZipCode(ZipCode zipCode) throws Exception 
	{
		int rc = -1;
		String query="INSERT INTO ZipCode(zipCode) VALUES('"+
		  		zipCode.getZipcode()  + "')";
		try
		{ 
	          Statement stmt = con.createStatement();
	          stmt.setQueryTimeout(5);
	     	  rc = stmt.executeUpdate(query);
	          stmt.close();
	    }
	    catch(SQLException ex)
	    {
	        System.out.println("ZipCode is not created");
	        throw new Exception ("ZipCode is not inserted correct");
	    }
	    return(rc);
	}

	public int updateZipCode(ZipCode zipCode) 
	{
		ZipCode ZipCodeObject = zipCode;
		int lol = -1;

		String query="UPDATE ZipCode SET "+
		 	  "ZipCode ='"+ ZipCodeObject.getZipcode()+ "', " +
		 	  "City ='"+ ZipCodeObject.getCity()+ "', " +
		      " WHERE ZipCode = '"+ ZipCodeObject.getZipcode() + "'";
              System.out.println("Update query:" + query);
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	 	lol  = stmt.executeUpdate(query);

	 	 	stmt.close();
		}
	 	catch(Exception ex)
	 	{
	 	 	System.out.println("Update exception in ZipCode db: " + ex);
	  	}
		return(lol);
	}

	public int deleteZipCode(String zipCode)
	{
        int lol = -1;

        String query="DELETE FROM ZipCode WHERE ZipCode = '"+ zipCode +"'";
        System.out.println(query);
	  	try
	  	{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	  	lol = stmt.executeUpdate(query);
	 	  	stmt.close();
  		}
   	    catch(Exception ex)
   	    {
	 	  	System.out.println("Delete exception in ZipCode db: "+ex);
   	    }
		return(lol);
	}
}
